package com.qms.tubu.activity.account;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.qms.tubu.R;
import com.qms.tubu.activity.BaseActivity;
import com.qms.tubu.fragment.personal.MyDateFragment;
import com.qms.tubu.widget.SuperScrollView;

/**
 * Created by Mad on 2015/7/23.
 */
public class PersonalActivity extends BaseActivity implements ViewTreeObserver.OnGlobalLayoutListener,ViewPager.OnPageChangeListener{
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private SuperScrollView mSuperView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);
        findView();
        initView();

    }

    public SuperScrollView getSuperView() {
        return mSuperView;
    }

    private void findView()
    {
        mViewPager= (ViewPager) this.findViewById(R.id.personal_view_pager);
        mTabLayout= (TabLayout) this.findViewById(R.id.personal_tab_layout);
        mSuperView= (SuperScrollView) this.findViewById(R.id.super_scroll_view);
    }
    private void initView()
    {
        mViewPager.setAdapter(new PersonalAdapter(getSupportFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);
        ViewTreeObserver observer=mTabLayout.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(this);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setOffscreenPageLimit(3);
    }

    @Override
    public void onGlobalLayout() {
        mSuperView.setRangeOffset(mTabLayout.getMeasuredHeight());
        if (Build.VERSION.SDK_INT<16) {
            mTabLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            mTabLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class PersonalAdapter extends FragmentPagerAdapter{
        private MyDateFragment myDateFragment;
        public PersonalAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if(position==0)
            {
                if(myDateFragment==null)
                    myDateFragment=new MyDateFragment();
                return myDateFragment;
            }
            else
                return new MyDateFragment();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "马上";
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

}
